import com.hjy.test.Clazz;
import com.hjy.test.Student;
import com.hjy.json.Json;
import com.hjy.json.Parser;
import org.junit.Test;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Main {

    @Test
    public void test01() {
        String json = "{\"hello\": [12,13.24,-12,\"world\"]}";
        Json json1 = Json.parseStringAsJson(json);
        System.out.println(json1.toString());
    }

    /**
     * 测试 json 文件转 Json 对象
     * Json 对象
     * @throws IOException
     */
    @Test
    public void test02() throws IOException, IllegalAccessException, NoSuchFieldException {
        InputStream stream = com.hjy.Main.class.getClassLoader().getResourceAsStream("test.json");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(stream);
        // 设置字节数组和长度
        byte[] b = new byte[1024];
        int len = 0;
        StringBuilder s = new StringBuilder();
        // 只要读取到,就输出
        while ((len = bufferedInputStream.read(b)) != -1) {
            s.append(new String(b, 0, len));
        }

        stream.close();
        bufferedInputStream.close();

        Json parse = Json.parseStringAsJson(s.toString());
        Json key = parse.getKey("data").getKey("total");
        System.out.println(key.toString()); // "2"
    }

    /**
     * 复杂对象转换为 json 字符串
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    @Test
    public void test03() throws NoSuchFieldException, IllegalAccessException {
        Clazz clazz = new Clazz();
        ArrayList<Student> students = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Student student = new Student();
            student.age = 10 + i;
            student.name = "何骏扬" + i;
            student.habits = new ArrayList<>();
            student.habits.add("吃饭" + i);
            student.habits.add("睡觉" + i);
            students.add(student);
        }
        clazz.setStudents(students);
        clazz.setName("软件20-7班");
        clazz.setNum(32);

        Json json = Json.parseObjectAsJson(clazz);
        System.out.println(json.toString());
    }

    /**
     * 测试json字符串转换成Bean
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     */
    @Test
    public void test04() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
        String json = "{\"habits\":[\"吃饭0\",\"睡觉0\"],\"name\":\"何骏扬0\",\"age\":10}";
//        Json json1 = Json.parseStringAsJson(json);
//        System.out.println(json1);
         Student bean = Json.toBean(json, Student.class);
         System.out.println(bean);
         json = "{\"num\":32,\"name\":\"软件20-7班\",\"students\":[{\"habits\":[\"吃饭0\",\"睡觉0\"],\"name\":\"何骏扬0\",\"age\":10},{\"habits\":[\"吃饭1\",\"睡觉1\"],\"name\":\"何骏扬1\",\"age\":11},{\"habits\":[\"吃饭2\",\"睡觉2\"],\"name\":\"何骏扬2\",\"age\":12},{\"habits\":[\"吃饭3\",\"睡觉3\"],\"name\":\"何骏扬3\",\"age\":13}]}\n";
        Clazz bean1 = Json.toBean(json, Clazz.class);
        System.out.println(bean1);
    }
}
